import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import eslintPlugin from 'vite-plugin-eslint'
// function escapeRegExp(str) {
//   return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // 这个函数用于转义正则表达式中的特殊字符
// }
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  //获取各种环境下的对应的变量
  const env = loadEnv(mode, process.cwd())
  console.log('env: ', env)
  return {
    plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
      eslintPlugin({
        cache: false,
        include: ['src/**/*.js', 'src/**/*.vue'],
        exclude: ['node_modules/**']
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    //代理跨域
    server: {
      open: true, //vite项目启动时自动打开浏览器
      hmr: true,
      port: 8072,
      proxy: {
        '/api': {
          target: 'http://rap2api.taobao.org/app/mock/299869/',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    }
  }
})
